<?php
/**
 * The controller for User Information.
 * 
 */
require_once(APP.'libs'.DS.'util.php');
require_once(APP.'libs'.DS.'system_properties.php');
App::import('Vendor','/infolex/rest_wrapper');
class ChangesController extends AppController
{
	var $helpers = array('javascript','ToolTip');
	var $layout='';
	var $uses=array('User','Change');
	var $components=array('Account','Upload');
	
	// 兑换产品列表
	function getProductList($curPage=1,$time=null)
	{
		$this->__getProduct($curPage);
		$this->render(null,'management_layout','/changes/changes_free_list');
		return;
		
		
	}
	// ajax刷新兑换产品列表
	function getAjaxProductList($curPage)
	{
		$this->__getProduct($curPage);
		$this->render('/changes/ajax_changes_free_list');
		return;
		
		
	}
	/*
	 * 
	 *分页查询产品列表
	 * 参数curPage为当前页
	 * 参数isCharge 是否收费 默认为0 免费
	 */ 
	
	function __getProduct($curPage,$isCharge=0){
		
			$count=$this->Change->findCount();
		if($count==0){
			$this->set("productList",null);
			$this->set("curPage",0);
			$this->set("totalPage",0);
			return;
		}
		$number=SystemProperties::$product_pageNumber;
		$page=intval($count/$number);
		if($count%$number!=0){
			$page+=1;
		}
		if($curPage>$page){
			$curPage=$page;
		}else if($curPage<=0){
			$curPage=1;
		}
		$prenum=($curPage-1)*$number;
		$this->set("productList",$this->Change->selectChangesInfo($prenum));
		$this->set("curPage",$curPage);
		$this->set("isCharge",$isCharge);
		$this->set("totalPage",$page);
	}
	function updateProductInList($pid = 0,$in_list = 0,$page=1){
		$ids='';
		if (empty($pid)){
			echo 'Error Set Product。';
			exit();
		}
		
		$this->Change->updateProductInList($pid,$in_list);
		$this->redirect(SystemProperties::$app_host."/changes/getProductList/{$page}/".time());
	}	
	
	
	/*
	 * 删除产品
	 */
    function deletePruduct(){
		$ids='';
		if(!empty($this->data)){
			foreach($this->data as $key=>$value){//$key 对应产品的id value=1表示是要删除的
				if($value==1){
					$ids.=$key.",";
				}
			}
			$ids=substr($ids,0,-1);
			if(!empty($ids)){
				$data=$this->Change->selectMoreProduct($ids);
				foreach($data as $key=>$product){
					for($i=0;$i<6;$i++){
						$imgkey=SystemProperties::$img_names[$i];
						if(!empty($product['g_changes'][$imgkey])){
							$this->Upload->removePhoto($product['g_changes'][$imgkey]);
						}
					}
				}
				$this->Change->deleteProduct($ids);
			}
		}
		$this->redirect(SystemProperties::$app_host."/changes/getProductList/1/".time());
		return;
	}
	/*
	 * 点击修改产品链接跳转到修改产品页面
	 */
	function modifyChanges($isCharge=0,$id=null){
		$this->set("changes_action","/changes/add");
		if(!empty($id)){
			$data=$this->Change->selectProduct($id);
			$this->data=$data[0];
			$this->set("changes_action","/changes/modify");
		}
		
			if(!empty($id)){
				$this->set("product_title","修改兑换产品");
			}else{
				$this->set("product_title","添加兑换产品");
			}
		$this->render(null,'management_layout','/changes/changes_modify');
		return;
	}
	/*
	 * 修改产品
	 */
	function modify(){
		uses('sanitize');
		$sanitize=new Sanitize();
		$id=$this->data['g_changes']['g_id'];
		if(empty($this->data['g_changes']['g_name'])){
			$name='';
		}else{
			$name=$sanitize->escape($this->data['g_changes']['g_name']);
		}
		if(empty($this->data['g_changes']['g_brand'])){
			$g_brand='';
		}else{
			$g_brand=$sanitize->escape($this->data['g_changes']['g_brand']);
		}
		if(empty($this->data['g_changes']['g_jiazhi'])){
			$g_jiazhi=0;
		}else{
			$g_jiazhi=$sanitize->escape($this->data['g_changes']['g_jiazhi']);
		}
		if(empty($this->data['g_changes']['g_number'])){
			$g_number=0;
		}else{
			$g_number=$sanitize->escape($this->data['g_changes']['g_number']);
		}
		if(empty($this->data['g_changes']['g_description'])){
			$g_description='';
		}else{
			$g_description=$sanitize->escape($this->data['g_changes']['g_description']);
		}
		if(empty($this->data['g_changes']['g_exchanges'])){
			$g_exchanges=0;
		}else{
			$g_exchanges=$sanitize->escape($this->data['g_changes']['g_exchanges']);
		}
		if(empty($this->data['g_changes']['g_hot'])){
			$g_hot=0;
		}else if(intval($this->data['g_changes']['g_hot'])!=0 ){
			$g_hot=1;
		}else{
			$g_hot=0;
		}
		if(empty($this->data['g_changes']['g_protal_url'])){
			$g_protal_url='';
		}else{
			$g_protal_url=$sanitize->escape($this->data['g_changes']['g_protal_url']);
		}
		if(empty($this->data['g_changes']['g_extra_url'])){
			$g_extra_url='';
		}else{
			$g_extra_url=$sanitize->escape($this->data['g_changes']['g_extra_url']);
		}		
		if(empty($this->data['g_changes']['g_address'])){
			$g_address='';
		}else{
			$g_address=$sanitize->escape($this->data['g_changes']['g_address']);
		}
		if (!empty($this->data['g_changes']['is_coupon_show'])||$this->data['g_changes']['type']=='coupon') {
		    $is_coupon_show = 1;
		    $g_number = 9999;
		}	else {
		    $is_coupon_show = 0;
		}
		if(empty($this->data['g_changes']['g_order'])){
			$g_order = 0;
		}else{
			$g_order = intval($this->data['g_changes']['g_order']);
		}			
		if(empty($this->data['g_changes']['g_exchanges_gold'])){
			$g_exchanges_gold = 0;
		}else{
			$g_exchanges_gold = intval($this->data['g_changes']['g_exchanges_gold']);
		}			
		$sql='';
		for($i=0;$i<6;$i++){
			$img="g_img".$i;
			if(!empty($this->data['g_changes'][$img]['name'])){
				$info = $this->data['g_changes'][$img];
				$imgkey=SystemProperties::$img_names[$i];
				$target_name = time().$i.md5($info['name']);
				$target_name = $this->Upload->uploadPhoto($info,$target_name);
				$sql.="$imgkey='$target_name',";

			}
		}
		
		$photo=$this->Change->selectProduct($id);

		$sql.="g_extra_url='$g_extra_url',g_exchanges_gold='$g_exchanges_gold',g_order='$g_order',is_coupon_show='$is_coupon_show',g_name='$name',g_brand='$g_brand',g_jiazhi=$g_jiazhi,g_exchanges=$g_exchanges,g_number=$g_number,g_description='$g_description',g_hot=$g_hot,g_protal_url='$g_protal_url',g_address='$g_address'";
		$this->Change->updateProduct($id,$sql);
		$this->redirect(SystemProperties::$app_host."/changes/getProductList/1/".time());
		return;
	}
	/*
	 * 添加产品
	 */
    function add(){
		if(empty($this->data['g_changes']['g_id'])){
			unset($this->data['g_changes']['g_id']);
		}
		
		for($i=0;$i<6;$i++){
			$img="g_img".$i;
			if(!empty($this->data['g_changes'][$img]['name'])){
				$info = $this->data['g_changes'][$img];
				$imgkey=SystemProperties::$img_names[$i];
				$target_name = time().$i.md5($info['name']);
				$target_name = $this->Upload->uploadPhoto($info,$target_name);
				//$sql.="$imgkey='$target_name',";
				$this->data['g_changes'][$imgkey] = $target_name;

			}
		}
		

    	if(empty($this->data['g_changes']['g_jiazhi'])){
			$this->data['g_changes']['g_jiazhi']=0;
		}
		if(empty($this->data['g_changes']['g_number'])){
			$this->data['g_changes']['g_number']=0;
		}
		if(empty($this->data['g_changes']['g_hot'])){
			$this->data['g_changes']['g_hot']=0;
		}
    	if(empty($this->data['g_changes']['g_exchanges'])){
			$this->data['g_changes']['g_exchanges']=0;
		}
    	if(empty($this->data['g_changes']['g_exchanges_gold'])){
			$this->data['g_changes']['g_exchanges_gold']=0;
		}		
    	if(empty($this->data['g_changes']['type'])){
			$this->data['g_changes']['type']='kuaidi';
		}		
		if(empty($this->data['g_changes']['is_coupon_show'])){
			$this->data['g_changes']['is_coupon_show']=0;
		}
		
		if ($this->data['g_changes']['is_coupon_show']!=0 ||$this->data['g_changes']['type']=='coupon') {
		    $this->data['g_changes']['is_coupon_show'] = 1;
		    $this->data['g_changes']['type']='coupon';
		    $this->data['g_changes']['g_number'] = 9999;
		}		
		$ret = $this->Change->save($this->data['g_changes'],false);
		$this->redirect(SystemProperties::$app_host."/changes/getProductList/1/".time());
	}

	
}
